<template>
  <div class="total-box">
    <!-- 左侧树结构 -->
    <!--<div class="head-container">
      <el-tree
        style="height: 100%"
        :data="deptOptions"
        :props="defaultProps"
        :expand-on-click-node="false"
        ref="tree"
        highlight-current
        @node-click="handleNodeClick"
      />
    </div>-->

    <!-- 右侧 -->
    <div class="app-container">
      <el-row :gutter="20" style="height: 100%">
        <!--用户数据-->
        <el-col :span="24" :xs="24">
          <HkSearchform
            :data="queryParams"
            :field-list="filterInfo.fieldList"
            :list-type-info="dictList"
            @handleSearch="handleQuery"
            @handleReset="resetQuery"
            @handleEvent="handleEvent"
          />

          <el-row :gutter="10" style="margin-left:0">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新建</el-button>
          </el-row>

          <hk-table
            ref="tableRef"
            :loading="loading"
            :table-data="tableData"
            :table-columns="tableList"
            :selection="false"
            :show-index="true"
            :total="total"
            :operate="true"
            :table-height="80"
            :query-params="queryParams"
            :common-fixed="true"
            :operate-width="200"
            @handleSelectionChange="handleSelectionChange"
            @changePage="getList"
          >
            <template #operate="scope">
              <el-button size="mini" type="text" @click="handleUpdate(scope.row)">修改 </el-button>

              <el-button
                v-if="scope.row.state == 0"
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </hk-table>
        </el-col>
      </el-row>
    </div>
    <!-- 新增和编辑的对话框 -->
    <EditDialog ref="editDialogRef" :title="title" :dict-list="dictList" @handleAdd="getList" />
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import HkTable from '@/components/Hk-Table/index.vue'
import EditDialog from './component/emitDialog.vue'
import { deleteDatas, listAll, queryPageList, selectUnit } from '@/api/assets/network.js'

import HkSearchform from '@/components/HkSearchform/index.vue'
import DictLabel from '@/components/Common/dictLabel.vue'

const baseUrl = window.baseUrl.asset
export default {
  components: {
    Treeselect,
    HkTable,
    EditDialog,
    HkSearchform,
    DictLabel,
    selectUnit
  },
  data() {
    return {
      // 搜索项配置
      filterInfo: {
        fieldList: [
          { label: '名称', type: 'input', field: 'keyword' },
          {
            label: '网络类型 ',
            type: 'select',
            field: 'equipmentType',
            list: 'networkTypeList'
          },
          {
            label: '防护等级',
            type: 'select',
            field: 'equipmentProtection',
            list: 'equipmentProtectionList'
          },
          {
            label: '测评状态',
            type: 'select',
            field: 'evaluationStatus',
            list: 'evaluationStatusList'
          }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 表格数据
      tableData: [{}],
      //  表格
      tableList: [
        { label: '名称', param: 'name', width: '130px' },
        { label: '所属单位', param: 'equipmentOrgName', width: '130px' },
        { label: '类型', param: 'equipmentTypeDictLable', width: '130px' },
        { label: '涉密等级', param: 'equipmentSecLevelDictLable', width: '130px' },
        { label: '防护等级', param: 'equipmentProtectionDictLable', width: '300px' },
        { label: '测评状态', param: 'evaluationStatusDictLable', width: '130px' },
        { label: '运维负责人', param: 'maintenancePerson', width: '200px' },
        { label: '联系电话', param: 'maintenanceModel', width: '200px' },
        { label: '描述', param: 'describes', width: '200px' }
      ],
      // 弹出层标题
      title: '',
      // 部门树选项
      deptOptions: undefined,
      // 树结构参数
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 列表规则
      dictList: {
        // 设备类型
        networkTypeList: [],
        // 使用状态
        equipmentProtectionList: [],
        // 涉密等级
        equipmentSecLevelList: [],
        // 测评状态
        evaluationStatusList: [],
        evaluationStatusListList: [],
        // 安全域
        assetsSecDomainAutoList: [],
        // 部门
        orgList: []
      }
    }
  },

  created() {
    this.getDict()
    this.getList()
    this.getTreeselect()
  },
  methods: {
    // 获取所有字典
    getDict() {
      return new Promise((resolve, reject) => {
        listAll('networkType,equipmentSecLevel,equipmentProtection,evaluationStatus,assetsSecDomainAuto').then(
          (res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            resolve(this.dictList)
          }
        )
      })
    },
    /** 查询列表 */
    getList(val) {
      this.loading = true
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }
      queryPageList(this.queryParams).then((res) => {
        this.total = res.total
        this.tableData = res.list
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },
    /** 查询单位*/
    getTreeselect() {
      selectUnit().then((res) => {
        this.dictList.orgList = res
      })
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getList({ orgId: data.id })
    },

    /** 搜索按钮操作 */
    handleQuery(val) {
      this.queryParams.current = 1
      this.getList()
    },
    // 重置表单
    resetQuery() {
      for (const key in this.queryParams) {
        this.queryParams[key] = undefined
      }
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getList()
    },
    // 搜索框变化
    handleEvent(val) {
      const field = val.field
      const value = val.value
      this.queryParams = {
        ...this.queryParams,
        ...{
          [field]: value
        }
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.title = '添加'
      this.$refs.editDialogRef.show()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑'
      this.$refs.editDialogRef.show(row.id)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteDatas(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
        })
        .catch((error) => {
          console.log(error)
        })
    },

    // 修改状态
    handleStatusChange(row) {}
  }
}
</script>
<style lang="scss" scoped>
.total-box {
  font-size: 14px;
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container {
  overflow: auto;
  padding: 20px;
  height: 100%;
  width: 270px;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
}
.app-container {
  // flex: 1;
}
.el-col {
  height: 100%;
  display: flex;
  flex-direction: column;
  &::v-deep .targetTable {
    flex: 1;
    overflow: auto;
  }
}
</style>
